package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.IovApiService;
import com.molichuxing.gateway.bff.car.entity.request.modify.WarnConfigModifyVo;
import com.molichuxing.gateway.bff.car.services.WarnConfigBffService;
import com.molichuxing.gateway.bff.permission.entity.response.AccountLoginVo;
import com.molichuxing.gateway.bff.permission.entity.response.IovAccountCreateVo;
import com.molichuxing.gateway.bff.permission.entity.response.IovAccountModifyVo;
import com.molichuxing.gateway.bff.permission.entity.response.IovControlVehicleCreateVo;
import com.molichuxing.gateway.bff.permission.services.AccountBffService;
import com.molichuxing.services.property.SystemTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 车联网相关接口
 * @author xuelin yi
 */
@Api(tags = "车联网相关接口")
@RestController
@RequestMapping(value = "/iov")
public class IovApiServiceImpl extends RootApiService implements IovApiService {

	private static final Logger logger = LoggerFactory.getLogger(IovApiServiceImpl.class);

	@Resource
	private AccountBffService accountBffService;

	@Resource
	private WarnConfigBffService warnConfigBffService;

	@Override
	@ApiOperation(value = "车联网控车成功发送短信接口（车联网）")
    @PostMapping(value = "/control/car")
	public ResponseResult<Boolean> sendMessageIovControlCar(@RequestBody IovControlVehicleCreateVo createVo) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		result.success(true);
		result.setMessage("成功");
		try {
//			result.success(violationBffService.getCarsViolationPaged(params, pageNum, pageSize));
			//TODO 发送短信
		} catch (Exception e) {
			// 记录异常，返回异常信息
			logger.error("[sendMessageIovControlCar]车联网控车成功发送短信接口", e);
			result.error("车联网控车成功发送短信接口:" + e.getMessage());
		}
		return result;
	}

	@Override
	@ApiOperation(value = "修改密码（车联网）")
	@PostMapping(value = "/account/modifyPasswordByIov")
	public ResponseResult<Boolean> modifyPasswordByIov(@RequestBody IovAccountCreateVo vo) {
		try {
			return accountBffService.modifyPasswordByIov(vo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ResponseResult<Boolean>().error("失败", Boolean.FALSE);
	}

	@Override
	@ApiOperation(value = "添加用户（车联网）")
	@PostMapping(value = "/account/createAccountByIov")
	public ResponseResult<Boolean> createIovAccount(@RequestBody IovAccountCreateVo vo) {
		return accountBffService.createIovAccount(vo);
	}

	@Override
	@ApiOperation(value = "修改用户（车联网）")
	@PostMapping(value = "/account/modifyAccountByIov")
	public ResponseResult<Boolean> modifyIovAccount(@RequestBody IovAccountModifyVo vo) {
		return accountBffService.modifyIovAccount(vo);
	}

	/**
	 * 账户登录
	 *
	 * @param params
	 * @return
	 */
	@Override
	@ApiOperation(value = "账户登录")
	@ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String")})
	@PostMapping(value = "/loginByIov")
	public ResponseResult<AccountLoginVo> login(@RequestBody Map<String, Object> params) {
		ResponseResult<AccountLoginVo> result = new ResponseResult<>();
		try {
			params.put("systemType", SystemTypeEnum.OP);
			result.success(accountBffService.loginByIov(params));
		} catch (BizException e) {
			logger.error("[login]登录异常:", e);
			result.error(e.getExceptionCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("[login]登录异常:", e);
			result.error("登录异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "修改车辆离线报警配置")
	@PostMapping(value = "/car/offline/warn/config/modify")
	public ResponseResult<Boolean> modifyIovCarOfflineWarnConfig(@RequestBody WarnConfigModifyVo vo) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			result.success(warnConfigBffService.modifyWarnConfig(vo));
		} catch (BizException e) {
			logger.error("[modifyIovCarOfflineWarnConfig]修改车辆离线报警配置异常:", e);
			result.error(e.getExceptionCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("[modifyIovCarOfflineWarnConfig]修改车辆离线报警配置异常:", e);
			result.error("修改车辆离线报警配置异常");
		}
		return result;
	}


}
